﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using System.Threading;

namespace TaxiDispatchClient
{
    public partial class ImageInfoForm : DevExpress.XtraEditors.XtraForm
    {
        public ImageInfoForm()
        {
            InitializeComponent();
        }
        public ImageInfoForm(ImageForm imgform)
        {
            InitializeComponent();
            string filepath = imgform.listView1.SelectedItems[0].Name;
            bindInfo(filepath);

            //将listview1中的图片地址存入list中
            for (int i = 0; i < imgform.listView1.Items.Count; i++)
            {
                ListViewItem lvitem = imgform.listView1.Items[i];
                if (lvitem.Name != null) 
                {
                    imagepathlist.Add(lvitem.Name);
                }
                
            }
            if(imgform.listView1.Items.Count==1)
            {
                btn_next.Enabled = false;
                btn_prev.Enabled = false;
            }
            currentindex = imagepathlist.IndexOf(filepath);
            if(currentindex ==0)
            {
                btn_prev.Enabled = false;
            }
            if(currentindex==imagepathlist.Count-1)
            {
                btn_next.Enabled = false;
            }
            label_current.Text = (currentindex+1).ToString();
            label_total.Text = imagepathlist.Count.ToString(); 

            
        }
        private List<string> imagepathlist = new List<string>();
        private int currentindex = 0;

        private void bindInfo(string filepath)
        {
            try
            {
                if (!File.Exists(filepath))
                {
                    return; 
                }
                //将图片大小调整为pictureEdit的大小显示在pictureEdit1中
                Image img =  Image.FromFile(filepath);
                Rectangle oldRectangle;
                Bitmap bmp;
                bmp = (Bitmap)img;
                oldRectangle=new Rectangle(0,0,bmp.Width,bmp.Height);
                Bitmap tmpbmp = new Bitmap(pictureEdit1.Width,pictureEdit1.Height);
                Graphics graphics = Graphics.FromImage(tmpbmp);
                Rectangle  newRectangle = new Rectangle(0,0,tmpbmp.Width,tmpbmp.Height);
                graphics.DrawImage(bmp,newRectangle,oldRectangle,GraphicsUnit.Pixel);
                pictureEdit1.Image = tmpbmp;
                graphics.Dispose();
                string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);
                string taxino = filename.Remove(filename.IndexOf('_'));
                DataSet ds = MainForm.PositionService.DriverGetListWithPar("taxino", taxino);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    DataRow row = ds.Tables[0].Rows[0];
                    lb_taxino.Text = taxino;
                    lb_driver.Text = row["Name"].ToString();
                    lb_phone.Text = row["PhoneNumber"].ToString();
                    lb_taxitype.Text = row["taxitype"].ToString();
                    lb_workid.Text = row["WorkId"].ToString();
                    lb_filename.Text = filename;

                }
                string time = filename.Substring(filename.LastIndexOf("_") + 1);
                time = time.Insert(4, "年").Insert(7, "月").Insert(10,
                    "日").Insert(13, "点").Insert(16, "分").Insert(19, "秒");
                time = time.Remove(time.LastIndexOf('.'));
                lb_time.Text = time;
            }
            catch (Exception ex)
            {

                Common.CommonRoutine.HandlingExceptions(ex, true);
                return;

            }
        
        }

        private void btn_prev_Click(object sender, EventArgs e)  //  上一张
        {
            if (currentindex > 0)
            {
                currentindex = currentindex - 1;
                bindInfo(imagepathlist[currentindex].ToString());
                if (currentindex == 0) 
                    btn_prev.Enabled = false;
                if (currentindex == imagepathlist.Count - 1)
                    btn_next.Enabled = false;
                else
                    btn_next.Enabled = true;  
            } 
            else
                btn_prev.Enabled = false;
            label_current.Text = (currentindex+1).ToString();
             
        }

        private void btn_next_Click(object sender, EventArgs e)  //  下一张
        {
            if (currentindex < imagepathlist.Count - 1)
            {
                currentindex = currentindex + 1;
                bindInfo(imagepathlist[currentindex].ToString());
                if (currentindex == imagepathlist.Count - 1)
                    btn_next.Enabled = false;
                btn_prev.Enabled = true;
            }
            else
                btn_next.Enabled = false;

            label_current.Text = (currentindex+1).ToString();
        }
        private void btn_MouseHover(object sender, EventArgs e)
        {
            Cursor = Cursors.Hand;
        }

        private void btn_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Arrow;
        }
        Point _mouseOff;
        bool _leftFlag;
        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _mouseOff = new Point(-e.X, -e.Y);
                _leftFlag = true;
            }
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(_mouseOff.X, _mouseOff.Y);
                Location = mouseSet;
            }
        }

        private void Form_MouseUp(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                _leftFlag = false;
            }
        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Close();
        }
        private void pictureEdit_exit_MouseHover(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexit;
            Cursor = Cursors.Hand;
        }

        private void pictureEdit_exit_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexitPressDown;
            Cursor = Cursors.Arrow;
        }
        private void Form_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                Close();
            }
        }
    }
}